package com.song.EMS.mvc;

import java.io.IOException;
import java.io.InputStream;
import java.lang.annotation.Annotation;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

/**
 * 映射池
 *
 * @author song
 */
public class HeaderMapping {
    /**
     *  Mapping 存某个对象 该对象某个种类的方法， String 这个方法的的注解值value
     */
    public static Map<String, Mapping> map = new HashMap<>(10);

    /**
     * 初始化加载属性文件
     * @param is properties 文件数据流
     */
    public static void load(InputStream is) {
        try {
            //加载初始化
            Properties properties = new Properties();
            properties.load(is);
            //获得properties文件中所内容
            Collection<Object> values = properties.values();
            if (values != null) {
                for (Object value : values) {
                    //获取每个对象的类名，通过反射得到对象与方法
                    Class aClass = Class.forName((String) value);
                    //获取对象
                    Object o = aClass.getConstructor().newInstance();
                    //获取每个对象中所有的方法
                    Method[] methods = aClass.getMethods();
                    if (methods != null && methods.length > 0){
                        for (Method method : methods) {
                            //获取方法的注解
                            Annotation[] annotation = method.getAnnotations();
                            if (annotation != null){
                                for (Annotation an : annotation) {
                                    //给前台返回文本信息
                                    if (an instanceof ResponseBody){
                                        String methodAnnValue = ((ResponseBody) an).value();
                                        System.out.println(methodAnnValue);
                                        Mapping mapping = new Mapping(o,method,DispatcherType.TEXT);
                                        Mapping put = map.put(methodAnnValue, mapping);
                                        if (put != null) {
                                            //添加重复
                                           throw new RuntimeException("请求地址重复"+methodAnnValue);
                                        }
                                        //跳转页面
                                    }else if (an instanceof ResponseView){
                                        String methodAnnValue = ((ResponseView) an).value();
                                        Mapping mapping = new Mapping(o,method,DispatcherType.VIEW);
                                        //Map集合添加成功就会有返回值，返回null，说明新增，否则就是覆盖旧值
                                        Mapping put = map.put(methodAnnValue, mapping);
                                        if (put != null) {
                                            //添加重复
                                            throw new RuntimeException("请求地址重复"+methodAnnValue);
                                        }
                                    }
                                }
                            }

                        }
                    }

                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        }


    }

    /**
     * 通过地址获取映射
     * @param uri 请求地址
     * @return 映射对象
     */
    public static Mapping getMapping(String uri) {
        return map.get(uri);
    }
}
